﻿var schedule = require("node-schedule");
var rule = new schedule.RecurrenceRule();

var times = [];

for (var i = 1; i < 60; i+=5) {

    times.push(i);

}

rule.second = times;

var c = 0;
var j = schedule.scheduleJob(rule, function () {
    c++;
    console.log(c);
	console.log(new Date());
	A();
});




function A(){
	var log="执行的时间："+new Date().toString();
	try{	
	console.log("h");
	var http = require('http');
	var querystring = require('querystring');
	 
	var contents = querystring.stringify({
		key:'pwc221_1qaz2wsx'
	});
	 
	var options = {
		//host:'localhost',
		host:'hkappuwv223',
		path:'/SmartOfficeBookingSupport/API/RunjobAPI?key=pwc221_1qaz2wsx',		
		method:'POST',
		headers:{
			'Content-Type':'application/x-www-form-urlendcoded',
			'Content-Length':contents.length
		}
	}
	 
	var req = http.request(options, function(res){
		res.setEncoding('utf8');
		res.on('data',function(data){
			console.log("data:",data);   //一段html代码
			log +=",执行结果:"+data;
			WriteLog(log);
			
		});
	});
	 
	req.write(contents);
	req.end;
	}
	catch(e){
	    log +=",执行结果:异常";
		WriteLog(log);
		req.write("error");
		req.end;
	}
}

function WriteLog(data){
	var fs = require("fs");	
	fs.writeFile('runLog.txt',data,{flag:'w',encoding:'utf-8',mode:'0666'},function(err){
		 if(err){
			 console.log("文件写入失败")
		 }else{
			 console.log("文件写入成功");
		 }
	}) 
}